﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmMainWindows : Form
    {
        private PhanQuyenMgt phanquyenMgt = new PhanQuyenMgt();
        private ChucNangMgt chucnangMgt = new ChucNangMgt();
        private ChucVuMgt chucvuMgt = new ChucVuMgt();

        private bool isNguoiKiemTra = false; 
        public NhanVien nhanvien = new NhanVien();

        public FrmMainWindows()
        {
            InitializeComponent();
        }

        public void BindingPrivillege()
        {
            PhanQuyen phanquyen = new PhanQuyen();
            phanquyen.IdChucVu = nhanvien.IdChucVu;

            List<PhanQuyen> lstPhanQuyen = new List<PhanQuyen>();
            lstPhanQuyen = GetPhanQuyenByIdChucVu(phanquyen);

            for (int i = 0; i < lstPhanQuyen.Count; i++)
            {
                ChucNang chucnang = new ChucNang();
                chucnang.Id = lstPhanQuyen[i].IdChucNang;

                ResultObj resultObj = new ResultObj();

                resultObj = chucnangMgt.GetChucNangById(chucnang);
                List<ChucNang> lstChucNang = new List<ChucNang>();

                lstChucNang = (List<ChucNang>)resultObj.Result;
                ToolStripMenuItem item = (ToolStripMenuItem)menuStrip1.Items[lstChucNang[0].Nhom];

                if (lstPhanQuyen[i].Quyen == 1)
                    item.DropDownItems[lstChucNang[0].MaSo].Enabled = true;
            }

            ToolStripMenuItem itemXuatHang = (ToolStripMenuItem)menuStrip1.Items["btnXuatHang"];
            if (itemXuatHang.DropDownItems["btnDuyetPhieuXuatHang"].Enabled == true && itemXuatHang.DropDownItems["btnPhieuXuatHang"].Enabled == false)
            {
                itemXuatHang.DropDownItems["btnDuyetPhieuXuatHang"].Text = "Chuẩn bị xuất hàng";
                isNguoiKiemTra = true;
            }
            else
            {
                itemXuatHang.DropDownItems["btnDuyetPhieuXuatHang"].Text = "Duyệt phiếu xuất bán hàng";
                isNguoiKiemTra = false;
            }

            ToolStripMenuItem itemHeThong = (ToolStripMenuItem)menuStrip1.Items["btnHeThong"];
            itemHeThong.DropDownItems["btnDangNhap"].Enabled = false;
            itemHeThong.DropDownItems["btnDangXuat"].Enabled = true;
            itemHeThong.DropDownItems["btnThoat"].Enabled = true;
        }

        void frmLogin_formCloseLogin(NhanVien nv)
        {
            this.nhanvien = nv;
            CheckingUser();
        }

        public void Login()
        {
            FrmLogin frmLogin = new FrmLogin();
            frmLogin.formCloseLogin += new FormCloseLoginEventHandler(frmLogin_formCloseLogin);
            frmLogin.ShowDialog();
        }

        public void CheckingUser()
        {
            if (this.nhanvien.Id != 0)
            {
                ChucVu chucvu = new ChucVu();
                chucvu.Id = nhanvien.IdChucVu;

                ResultObj resultObj = new ResultObj();

                resultObj = chucvuMgt.GetChucVuById(chucvu);
                List<ChucVu> lstChucVu = (List<ChucVu>)resultObj.Result;

                this.Text = string.Format("Người dùng: {0} - Chức vụ: {1}", nhanvien.HoTen, lstChucVu[0].TenChucVu);

                BindingPrivillege();
            }
            else
            {
                foreach (ToolStripMenuItem item in menuStrip1.Items)
                {
                    foreach (ToolStripMenuItem t in item.DropDownItems)
                    {
                        if (t.Name != "btnDangNhap")
                            t.Enabled = false;
                        else
                            t.Enabled = true;
                    }
                }
            }
        }

        private void FrmMainWindows_Load(object sender, EventArgs e)
        {
            Login();
            CheckingUser();
        }

        private void btnChiNhanhCty_Click(object sender, EventArgs e)
        {
            FrmQLCongTy frmChiNhanhCongTy = new FrmQLCongTy();
            frmChiNhanhCongTy.ShowDialog();
        }

        private void btnNhanVien_Click(object sender, EventArgs e)
        {
            FrmThongTinNhanVien frmThongTinNhanVien = new FrmThongTinNhanVien();
            frmThongTinNhanVien.ShowDialog();
        }

        private void btnKhachHang_Click(object sender, EventArgs e)
        {
            FrmKhachHang frmKH = new FrmKhachHang();
            frmKH.ShowDialog();
        }

        private void btnNhaCungCap_Click(object sender, EventArgs e)
        {
            FrmQLNhaCungCap frmQLNhaCC = new FrmQLNhaCungCap();
            frmQLNhaCC.ShowDialog();
        }

        private void btnTaiKhoanKeToan_Click(object sender, EventArgs e)
        {
            FrmTaiKhoanKeToan frm = new FrmTaiKhoanKeToan();
            frm.ShowDialog();
        }

        private void btnNganHang_Click(object sender, EventArgs e)
        {
            FrmNganHang_TaiKhoan frm = new FrmNganHang_TaiKhoan();
            frm.ShowDialog();
        }

        private void btnHangHoa_Click(object sender, EventArgs e)
        {
            FrmQuanLyHangHoa frm = new FrmQuanLyHangHoa();
            frm.ShowDialog();
        }

        private void btnKhayDung_Click(object sender, EventArgs e)
        {
            FrmQLKhayDung frm = new FrmQLKhayDung();
            frm.ShowDialog();
        }

        private void btnBangKe_Click(object sender, EventArgs e)
        {
            FrmTimKiemBangKeNhapHang frm = new FrmTimKiemBangKeNhapHang(this.nhanvien);
            frm.ShowDialog();
        }

        private void btnDuyetBangKe_Click(object sender, EventArgs e)
        {
            FrmDuyetNhapHang frmDuyetNhapHang = new FrmDuyetNhapHang();
            frmDuyetNhapHang.NhanVien = this.nhanvien;
            frmDuyetNhapHang.ShowDialog();
        }

        private void btnPhieuNhapKho_Click(object sender, EventArgs e)
        {
            FrmTimKiemPhieuNhapKho frm = new FrmTimKiemPhieuNhapKho(this.nhanvien);
            frm.ShowDialog();
        }

        private void btnMuaHang_Click(object sender, EventArgs e)
        {
            FrmTimKiemMuaHang frmTimKiemMuaHang = new FrmTimKiemMuaHang(this.nhanvien);
            frmTimKiemMuaHang.ShowDialog();
        }

        private void btnSuaDonGiaNhap_Click(object sender, EventArgs e)
        {
            FrmCapNhatGia frmCapNhatGia = new FrmCapNhatGia();
            frmCapNhatGia.ShowDialog();
        }

        private void btnDangXuat_Click(object sender, EventArgs e)
        {
            this.Text = "Phần mềm quản trị tập trung";
            this.nhanvien = new NhanVien();

            CheckingUser();
            Login();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            Login();
        }

        private void btnPhieuXuatHang_Click(object sender, EventArgs e)
        {
            FrmTimKiemPhieuXuatHang frmTimPhieuXuatHang = new FrmTimKiemPhieuXuatHang();
            frmTimPhieuXuatHang.NhanVien = this.nhanvien;
            frmTimPhieuXuatHang.ShowDialog();
        }

        private void btnDuyetPhieuXuatHang_Click(object sender, EventArgs e)
        {
            FrmDuyetPhieuXuat frmDuyetPhieuXuatHang = new FrmDuyetPhieuXuat();
            frmDuyetPhieuXuatHang.NhanVien = this.nhanvien;
            frmDuyetPhieuXuatHang.isNguoiKiemTra = this.isNguoiKiemTra;
            frmDuyetPhieuXuatHang.ShowDialog();
        }

        private void btnChoCuocGoi_Click(object sender, EventArgs e)
        {
            FrmChoCuocGoi frm = new FrmChoCuocGoi();
            frm.NhanVien = this.nhanvien;
            frm.ShowDialog();
        }

        private void btnGoiMoiHang_Click(object sender, EventArgs e)
        {
            FrmGoiMoiHang frm = new FrmGoiMoiHang();
            frm.NhanVien = this.nhanvien;
            frm.ShowDialog();
        }

        private void btnPhieuXuatKho_Click(object sender, EventArgs e)
        {
            FrmTimPhieuXuatKho frm = new FrmTimPhieuXuatKho();
            frm.NhanVien = this.nhanvien;
            frm.ShowDialog();
        }

        private void btnKiemSoatLien3_Click(object sender, EventArgs e)
        {
            FrmKiemSoatLien3 frm = new FrmKiemSoatLien3();
            frm.NhanVien = this.nhanvien;
            frm.ShowDialog();
        }

        private void btnThuTienHang_Click(object sender, EventArgs e)
        {
            FrmThuTienHang frm = new FrmThuTienHang();
            frm.NhanVien = this.nhanvien;
            frm.ShowDialog();
        }

        private void btnThuKhac_Click(object sender, EventArgs e)
        {
            FrmPhieuThuTien frm = new FrmPhieuThuTien(this.nhanvien);
            frm.ShowDialog();
        }

        private void btnTimKiemPhieuThu_Click(object sender, EventArgs e)
        {
            FrmTimKiemPhieuThuTien frm = new FrmTimKiemPhieuThuTien();
            frm.NhanVien = this.nhanvien;
            frm.ShowDialog();
        }

        private void btnChiTienHang_Click(object sender, EventArgs e)
        {
            FrmChiTienHang frm = new FrmChiTienHang();
            frm.NhanVien = this.nhanvien;
            frm.ShowDialog();
        }

        private void btnChiKhac_Click(object sender, EventArgs e)
        {
            FrmPhieuChiTien frmPhieuChiTien = new FrmPhieuChiTien(this.nhanvien);
            frmPhieuChiTien.ShowDialog();
        }

        private void btnTimKiemPhieuChi_Click(object sender, EventArgs e)
        {
            FrmTimKiemPhieuChiTien frmTimKiemPhieuChi = new FrmTimKiemPhieuChiTien();
            frmTimKiemPhieuChi.NhanVien = this.nhanvien;
            frmTimKiemPhieuChi.ShowDialog();
        }

        private void btnPhieuKeToan_Click(object sender, EventArgs e)
        {
            FrmTimKiemPhieuKeToan frm = new FrmTimKiemPhieuKeToan();
            frm.NhanVien = this.nhanvien;
            frm.ShowDialog();
        }

        private void btnDoiTraHang_Click(object sender, EventArgs e)
        {
            FrmTimKiemDoiTraHang frm = new FrmTimKiemDoiTraHang(nhanvien);
            frm.ShowDialog();
        }

        private void btnPhieuChuyenKho_Click(object sender, EventArgs e)
        {
            FrmTimKiemPhieuChuyenKho frm = new FrmTimKiemPhieuChuyenKho();
            frm.NhanVien = this.nhanvien;
            frm.ShowDialog();
        }

        private void btnPhieuThanhToanTienHang_Click(object sender, EventArgs e)
        {
            FrmPhieuThanhToanTienHang frm = new FrmPhieuThanhToanTienHang();
            frm.ShowDialog();
        }

        private void btnKhauTruThue_Click(object sender, EventArgs e)
        {
            FrmTimKiemDoiTruThue frm = new FrmTimKiemDoiTruThue();
            frm.ShowDialog();
        }

        private void btnTheKho_Click(object sender, EventArgs e)
        {
            FrmTheKho frm = new FrmTheKho();
            frm.ShowDialog();
        }

        private void btnSoKeToan_Click(object sender, EventArgs e)
        {
            FrmBaoCaoKeToan frmBaoCaoKeToan = new FrmBaoCaoKeToan();
            frmBaoCaoKeToan.NhanVien = this.nhanvien;
            frmBaoCaoKeToan.ShowDialog();
        }

        private void btnCongNo_Click(object sender, EventArgs e)
        {
            FrmCongNo frm = new FrmCongNo();
            frm.ShowDialog();
        }

        private void btnBaoCaoQuy_Click(object sender, EventArgs e)
        {
            FrmBaoCaoQuy frm = new FrmBaoCaoQuy();
            frm.nhanvien = this.nhanvien;
            frm.ShowDialog();
        }

        private void btnXuatNhapTon_Click(object sender, EventArgs e)
        {
            FrmBCDXNT frm = new FrmBCDXNT();
            frm.ShowDialog();
        }

        private void btnBCDTK_Click(object sender, EventArgs e)
        {
            FrmBCDTK frm = new FrmBCDTK();
            frm.ShowDialog();
        }

        private void btnBCDKT_Click(object sender, EventArgs e)
        {
            FrmBCDKT frm = new FrmBCDKT();
            frm.ShowDialog();
        }

        private void btnTongHopNhapXuat_Click(object sender, EventArgs e)
        {
            FrmTongHopNhapXuatHang frm = new FrmTongHopNhapXuatHang();
            frm.ShowDialog();
        }

        private void btnPhanQuyen_Click(object sender, EventArgs e)
        {
            FrmPhanQuyen frm = new FrmPhanQuyen();
            frm.ShowDialog();
        }

        public List<PhanQuyen> GetPhanQuyenByIdChucVu(PhanQuyen phanquyen)
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phanquyenMgt.GetAllPhanQuyenByIDChucVu(phanquyen);

            List<PhanQuyen> lstPhanQuyen = new List<PhanQuyen>();
            lstPhanQuyen = (List<PhanQuyen>)resultObj.Result;

            return lstPhanQuyen;
        }

        public List<ChucVu> GetChucVuById(ChucVu chucvu)
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chucvuMgt.GetChucVuById(chucvu);

            List<ChucVu> lstChucVu = new List<ChucVu>();
            lstChucVu = (List<ChucVu>)resultObj.Result;

            return lstChucVu;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnChucVu_Click(object sender, EventArgs e)
        {
            FrmChucVu frm = new FrmChucVu();
            frm.ShowDialog();
        }

        private void btnMucChi_Click(object sender, EventArgs e)
        {
            FrmMucChi mc = new FrmMucChi();
            mc.ShowDialog();
        }

        private void btnUyNhiemChi_Click(object sender, EventArgs e)
        {
            FrmTimKiemUyNhiemChi frm = new FrmTimKiemUyNhiemChi();
            frm.NhanVien = this.nhanvien;
            frm.ShowDialog();
        }

        private void btnTKNH_Click(object sender, EventArgs e)
        {
            FrmNganHang_DoiTac frm = new FrmNganHang_DoiTac();
            frm.ShowDialog();
        }

        private void btnDonViTinh_Click(object sender, EventArgs e)
        {
            FrmDonViTinh dvt = new FrmDonViTinh();
            dvt.ShowDialog();
        }

        private void btnBangMaPhieu_Click(object sender, EventArgs e)
        {
            FrmBangMaPhieu frmBMP = new FrmBangMaPhieu();
            frmBMP.ShowDialog();
        }
    }
}
